<?php
/**
 *@desc:Magic library which having all basic needed functions used to run framwork. 
 */
include_once 'library/magic.lib.php';
/**
 *@desc : This is class is used to load other classes into one class .
 * So ,Logically it provide muliple inheritance functionality to the base class . 
 */
MagicPHP::callMethod()->import(LIB_PATH.'inc/Magic_ClassUpload_Model','inc');

/**
 *@desc: This is the base controller . Any controller in module must inherit this controller.
 * If not than functionality will not work properly . 
 */
MagicPHP::callMethod()->import(LIB_PATH.'base_controller');

MagicPHP::callMethod()->import(LIB_PATH.'inc/Magic_Varmaker_Lib','inc');
/**
 *@desc : This is the template class . Use of this class to render template into application. 
 */
MagicPHP::callMethod()->import(LIB_PATH.'template.lib');
/**
 * 
 */
MagicPHP::callMethod()->import(LIB_PATH.'session.lib');
/**
 *@desc : This is bootstrap class of application .
 * Use to handel all functionality of framework .
 * To work framework properly please do not modified this file . 
 */
MagicPHP::callMethod()->importLibarary('Magic_RedIniFile_Model');
MagicPHP::callMethod()->importLibarary('Magic_Connection_Model');
MagicPHP::callMethod()->importLibarary('Magic_Common_Model');
MagicPHP::callMethod()->import(LIB_PATH.'acl.lib',NULL,false);

/**
 *@desc : Role object  
 */


class bootstrap {
    /**
     *@desc : Protected variable to define name of model .
     */
    protected $model ;
    /**
     *@desc: Protected variable use to difine controller.
     */
    protected $controller ; 
    /**
     *@desc: Public variable use to difine view.
     */
    public $view ;
    /**
     *@desc: Public variable use to difine controller class of particular module.
     */
    public $controllerClass ;
    /**
     *@desc: Public variable use to define action event of request.
     */
    public $method ;
    
    /**
     *@desc: contructor of bootstrap define all variable on creation of object .
     */
    public function __construct($enable = false) {
        $aclFile = ROOT.DS."log".DS."application.acl";
        $con = file_get_contents($aclFile,FILE_USE_INCLUDE_PATH);  
        $role =json_decode($con,true);  
        global $aclObj ; 
       
        $aclObj = ACL::callMethod()->bindArrayToObject($role);
        
        $this->model = INCLUDE_PATH.MagicPHP::callMethod()->getAppDirectory().DS.M_D.DS.MagicPHP::callMethod()->getModel(); 
        $this->controller = INCLUDE_PATH.MagicPHP::callMethod()->getAppDirectory().DS.C_D.DS.MagicPHP::callMethod()->getController(); 
        $this->view = INCLUDE_PATH.MagicPHP::callMethod()->getAppDirectory().DS.V_D.DS.strtolower(MagicPHP::callMethod()->getView());
        $this->controllerClass = MagicPHP::callMethod()->getController();
        $this->method = MagicPHP::callMethod()->getViewAction();
    }
    /**
     *@desc: This class is used to include controller and module file which indicate by request url
     *@return \ErrorException 
     */
    public function autoLoader(){
        if($this->checkMVC($this->model.MagicPHP::$ext)){
            MagicPHP::callMethod()->import($this->model,'',false);
            if($this->checkMVC($this->controller.MagicPHP::$ext)){
                MagicPHP::callMethod()->import($this->controller,'',false);
            } else {
                return new ErrorException();
            }
        } elseif(!$this->checkMVC($this->model.MagicPHP::$ext)) {
            if($this->checkMVC($this->controller.MagicPHP::$ext)){
                MagicPHP::callMethod()->import($this->controller,'',false);
            } else {
                return new ErrorException();
            }
       }
    }
    /**
     *@desc: this function is used to check avability of particular file 
     * @param $filename: name of file 
     * @return boolean 
     */
    public function checkMVC($filename){
        if(file_exists($filename)){
            return true;
        } else {
            return false;
        }
    }
    /**
     * @desc : Used to redirect url on with index file  
     */
    public function bootStrapRedirect($enable = false ,$sessionPara = NULL ,$loginmodule = array()){
        if($_SERVER["REQUEST_URI"] == '/'){
            Magic_Common_Model::callMethod()->_pageRedirect("index.php");
        }
    }
    public function readINI($iniKey , $iniVal){
       return  Magic_ReadIniFile_Model::callMethod()->readIniValue(CONFIG_PATH,$iniKey,$iniVal);
    }
    public function isAllow($aclObj){
        if(ACL_ENABLE == true) {
            $m = str_replace("_model","",MagicPHP::callMethod()->getModel());
            $sId = Magic_Session_Lib::callMethod()->_getSession(ACL_ID);
            if(!is_object($aclObj->$sId) && $aclObj->$sId == 'all' ){
                return true ;
            } elseif(is_object($aclObj->$sId)) {
                if($aclObj->$sID->$m->{$this->method} === true ) {
                    return true ; 
                } else {
                    return false;
                }
            } else {
                return false ; 
            }
        } else {
            return true ; 
        }
        
         /*if($aclObj->$sID->$m->{$this->method} === true ) {
            
        } else {
            echo 'no';
        }*/
    }
}
/**
 *@desc : Object of bootstrap class . 
 */
$bootstrap = new bootstrap("false");
//$bootstrap->bootStrapRedirect();
$bootstrap->autoLoader();

/**
 *@desc :Site Configration 
 */
define("SITE_URL",$bootstrap->readINI('SITE-SETTING',"site_url"));
define("LOG_PATH",ROOT."/log.ini");
 
//exit  ;
if(MagicPHP::callMethod()->getBaseClass($bootstrap->controllerClass)) {
/**
 *@desc : Check controller Method exist 
 * If exist than do appropriate operation  
 */

if(method_exists($bootstrap->controllerClass,$bootstrap->method)){
    
   /**
    * initialy call contructor function to create object of the model 
    */
    $objC = new $bootstrap->controllerClass();
    MagicPHP::callMethod()->copyObj($objC);
    $ref = new ReflectionClass($objC);
    call_user_func_array(array($ref,'newInstanceArgs'),array());
    
   /**
    * Here Action Method Call from controller file  
    */
    call_user_func_array(array($objC,$bootstrap->method),array(''));   
    /**
     * here we import view file
     */
    //$m = str_replace("_model","",MagicPHP::callMethod()->getModel());
    if($bootstrap->isAllow($aclObj)) {
        /*if($aclObj->$_SESSION["role_id"]->$m[0]->{$bootstrap->method} == "true") {

                include("/var/www/domain/public/error/index.html");

        }*/
        try {
            include $bootstrap->view.".php";
        } catch (Exception $e){
            echo  $e->getMessage();
        } 
    } else {
        echo "<h1>404 Not found .</h1>";
    }
} else {
    
   echo  new ErrorException();
}
} else {
    echo new ErrorException("Please inherit base_controller in your controller class");
}
?>
